package cn.neo.hhsharding.demo;

import cn.neo.hhsharding.demo.mapper.UserMapper;
import cn.neo.hhsharding.demo.model.User;
import cn.neo.hhsharding.config.ShardingAutoConfiguration;
import cn.neo.hhsharding.mybatis.ShardingMapperFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@Import(ShardingAutoConfiguration.class)
@MapperScan(value = "cn.neo.hhsharding.demo.mapper", factoryBean = ShardingMapperFactoryBean.class)
public class HhshardingApplication {

    public static void main(String[] args) {
        SpringApplication.run(HhshardingApplication.class, args);
    }

    // 此Mapper由ShardingMapperFactoryBean代理生成
    @Autowired
    UserMapper userMapper;

    @Bean
    ApplicationRunner runner(){
        return run->{
            for (int i = 0; i < 30; i++) {
               test(i);
            }
        };
    }

    private void test(int id) {
        System.out.println(" ===> 1.test insert");
        userMapper.insert(new User(id,"neo",18));

        System.out.println(" ===> 2.test findById");
        User user = userMapper.findById(id);
        System.out.println(" ===> 2.resut="+user);

        System.out.println(" ===> 3.test update");
        int row = userMapper.update(new User(id, "hh", 20));
        System.out.println(" ===> 3.resut="+row);

        System.out.println(" ===> 4.test findById");
        User user1 = userMapper.findById(id);
        System.out.println(" ===> 4.resut="+user1);

        //            System.out.println(" ===> 5.test delete");
//            userMapper.deleteById(2);
    }

}
